<template>
  <div class="divBox">
    <div class="selCard mb14">
      <el-form
        :model="tableFrom"
        ref="searchForm"
        inline
        size="small"
        label-width="85px"
      >
        <el-form-item label="选择时间：">
          <el-date-picker
            v-model="timeVal"
            value-format="yyyy/MM/dd"
            format="yyyy/MM/dd"
            :picker-options="pickerOptions"
            type="daterange"
            placement="bottom-end"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            style="width: 280px;"
            @change="onchangeTime"
          />
        </el-form-item>
        <el-form-item label="搜索：" prop="keyword">
          <el-input
            v-model="tableFrom.keyword"
            @keyup.enter.native="getList(1)"
            placeholder="请输入用户UID用户昵称、标题"
            class="selWidth"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="small" @click="getList(1)"
            >搜索</el-button
          >
          <el-button size="small" @click="searchReset()">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <cards-data
      v-if="cardLists.length > 0"
      :card-lists="cardLists"
      :three="true"
    />
    <el-card>
      <div class="mb20">
        <el-button size="small" type="primary" @click="exports">导出</el-button>
      </div>
      <el-table v-loading="listLoading" :data="tableData.data" size="small">
        <el-table-column prop="bill_id" label="ID" min-width="50" />
        <el-table-column label="用户昵称" prop="nickname" min-width="150" />
        <el-table-column label="积分标题" prop="title" min-width="120" />
        <el-table-column label="积分变动" prop="number" min-width="90">
          <template slot-scope="scope">
            <span v-if="scope.row.pm == 1" style="color: #ff3b30;"
              >+{{ scope.row.number }}</span
            >
            <span v-if="scope.row.pm == 0" style="color: #82e493;"
              >-{{ scope.row.number }}</span
            >
          </template>
        </el-table-column>
        <el-table-column label="当前积分额度" prop="balance" min-width="90" />
        <el-table-column prop="mark" label="备注" min-width="150" />
        <el-table-column prop="create_time" label="添加时间" min-width="90" />
      </el-table>
      <div class="block">
        <el-pagination
          background
          :page-size="tableFrom.limit"
          :current-page="tableFrom.page"
          layout="total, prev, pager, next, jumper"
          :total="tableData.total"
          @size-change="handleSizeChange"
          @current-change="pageChange"
        />
      </div>
    </el-card>
  </div>
</template>
<script>
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
import {
  integralLogTitle,
  integralLogLst,
  signLogExport
} from "@/api/marketing";
import fileList from "@/components/exportFile/fileList";
import cardsData from "@/components/cards/index";
import createWorkBook from "@/utils/newToExcel.js";
import { roterPre } from "@/settings";
import timeOptions from "@/utils/timeOptions";
export default {
  name: "IntergralLog",
  components: { fileList, cardsData },
  data() {
    return {
      pickerOptions: timeOptions,
      timeVal: [],
      listLoading: true,
      roterPre: roterPre,
      tableData: {
        data: [],
        total: 0
      },
      tableFrom: {
        page: 1,
        limit: 20,
        keyword: "",
        date: ""
      },
      loading: false,
      cardLists: []
    };
  },
  watch: {},
  mounted() {
    this.getList("");
    this.getTitle();
  },
  methods: {
    /**重置 */
    searchReset() {
      this.timeVal = [];
      this.tableFrom.date = "";
      this.$refs.searchForm.resetFields();
      this.getList(1);
    },
    // 具体日期
    onchangeTime(e) {
      this.timeVal = e;
      this.tableFrom.date = e ? this.timeVal.join("-") : "";
      this.tableFrom.page = 1;
      this.getList("");
    },
    async exports() {
      let excelData = JSON.parse(JSON.stringify(this.tableFrom)),
        data = [];
      excelData.page = 1;
      let pageCount = 1;
      let lebData = {};
      for (let i = 0; i < pageCount; i++) {
        lebData = await this.downData(excelData);
        pageCount = Math.ceil(lebData.count / excelData.limit);
        if (lebData.export.length) {
          data = data.concat(lebData.export);
          excelData.page++;
        }
      }
      createWorkBook(
        lebData.header,
        lebData.title,
        data,
        lebData.foot,
        lebData.filename
      );
      return;
    },
    /**列表 */
    downData(excelData) {
      return new Promise((resolve, reject) => {
        signLogExport(excelData).then(res => {
          return resolve(res.data);
        });
      });
    },
    // 导出
    exportRecord() {
      signLogExport(this.tableFrom)
        .then(res => {
          const h = this.$createElement;
          this.$msgbox({
            title: "提示",
            message: h("p", null, [
              h("span", null, '文件正在生成中，请稍后点击"'),
              h("span", { style: "color: teal" }, "导出记录"),
              h("span", null, '"查看~ ')
            ]),
            confirmButtonText: "我知道了"
          }).then(action => {
            this.$router.push({ path: this.roterPre + "/group/exportList" });
          });
        })
        .catch(res => {
          this.$message.error(res.message);
        });
    },
    //头部
    getTitle() {
      integralLogTitle()
        .then(res => {
          this.cardLists = res.data;
        })
        .catch(res => {
          this.$message.error(res.message);
        });
    },
    // 列表
    getList(num) {
      this.listLoading = true;
      this.tableFrom.page = num ? num : this.tableFrom.page;
      integralLogLst(this.tableFrom)
        .then(res => {
          this.tableData.data = res.data.list;
          this.tableData.total = res.data.count;
          this.listLoading = false;
        })
        .catch(res => {
          this.listLoading = false;
          this.$message.error(res.message);
        });
    },
    pageChange(page) {
      this.tableFrom.page = page;
      this.getList("");
    },
    handleSizeChange(val) {
      this.tableFrom.limit = val;
      this.getList("");
    }
  }
};
</script>
<style scoped lang="scss">
.scollhide::-webkit-scrollbar {
  display: none; /* Chrome Safari */
}
</style>
